﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using HKEA.CMS.Core.IDAL.App;
using HKEA.CMS.Core.Models;
using HKEA.CMS.Core.DALFactory;
namespace HKEA.CMS.Core.DAL.App
{
    public class app_tblCPerson_CommMemberDAL:Iapp_tblCPerson_CommMember
    {
        HKEA_CMSEntities model;
        app_tblCPerson_CommMember commMember;
        private app_tblCPerson_CommMemberDAL()
        {
            model = DataAccess.GetModelInstance();
        }

        public static app_tblCPerson_CommMemberDAL GetInstance()
        {
            return new app_tblCPerson_CommMemberDAL();
        }

        public int InsertENT(app_tblCPerson_CommMember ent)
        {
            ent.CreationDate = DateTime.Now;
            ent.LastModifyDate = DateTime.Now;
            model.app_tblCPerson_CommMember.AddObject(ent);
            if (model.SaveChanges() > 0)
                return model.app_tblCPerson_CommMember.Max(a=>a.AppCperson_CommMemberUID);
            return 0;
        }

        public List<app_tblCPerson_CommMember> GetListByENT(app_tblCPerson_CommMember ent,bool AddDeletedForSelect)
        {
            var ret = model.app_tblCPerson_CommMember.Where(a=>a.AppCperson_CommMemberUID > 0);
            if (AddDeletedForSelect)
                ret = ret.Where(a=>a.Deleted==ent.Deleted);
            if (ent.AppCperson_CommMemberUID > 0)
                ret = ret.Where(a=>a.AppCperson_CommMemberUID==ent.AppCperson_CommMemberUID);
            if (ent.Cperson_CommMemberUID > 0)
                ret = ret.Where(a=>a.Cperson_CommMemberUID==ent.Cperson_CommMemberUID);
            if (ent.AppContactPersonUID > 0)
                ret = ret.Where(a=>a.AppContactPersonUID==ent.AppContactPersonUID);
            if (ent.AppCperson_CommMemberUID > 0)
                ret = ret.Where(a=>a.AppCperson_CommMemberUID==ent.AppCperson_CommMemberUID);
            if (ent.ContactPersonUID > 0)
                ret = ret.Where(a=>a.ContactPersonUID==ent.ContactPersonUID);
            if (ent.Cperson_CommMemberUID > 0)
                ret = ret.Where(a=>a.Cperson_CommMemberUID==ent.Cperson_CommMemberUID);
            return ret.ToList();
        }

        public int GetCountByENT(app_tblCPerson_CommMember ent)
        {
            throw new NotImplementedException();
        }

        public int UpdateENT(app_tblCPerson_CommMember ent)
        {
            var result = model.app_tblCPerson_CommMember.Where(a=>a.AppCperson_CommMemberUID==ent.AppCperson_CommMemberUID).FirstOrDefault();
            if (result != null)
            {
                result.AppContactPersonUID = ent.AppContactPersonUID;
                result.CommPositionUID = ent.CommPositionUID;
                result.CommTypeUID = ent.CommTypeUID;
                result.ContactPersonUID = ent.ContactPersonUID;
                result.Cperson_CommMemberUID = ent.Cperson_CommMemberUID;
                result.Deleted = ent.Deleted;
                result.EndDate = ent.EndDate;
                result.LastModifyDate = DateTime.Now;
                result.Remarks = ent.Remarks;
                result.StartDate = ent.StartDate;
                result.Year = ent.Year;
            }
            return model.SaveChanges();
        }

        public int DeleteENT(app_tblCPerson_CommMember ent)
        {
            var result = model.app_tblCPerson_CommMember.Where(a=>a.AppCperson_CommMemberUID==ent.AppCperson_CommMemberUID).FirstOrDefault();
            if (result != null)
                model.app_tblCPerson_CommMember.DeleteObject(result);
            return model.SaveChanges();
        }

        public List<CommitteeMemberList> GetList(app_tblCPerson_CommMember ent, bool AddDeletedForSelect)
        {
            var list = from comm in model.app_tblCPerson_CommMember
                       select new CommitteeMemberList()
                       {
                           KeyUID=comm.AppCperson_CommMemberUID,
                           AppCPUID =comm.AppContactPersonUID ?? 0,
                           AppKeyUID = comm.AppCperson_CommMemberUID,
                           mtblType=comm.mtblCommType,
                           mtblPosition=comm.mtblCommPosition,
                           EndDate = comm.EndDate,
                           From = "app",
                           IsDeleted = comm.Deleted,
                           Remarks = comm.Remarks,
                           StartDate = comm.StartDate,
                           TblCPUID = comm.ContactPersonUID ?? 0,
                           TblKeyUID = comm.Cperson_CommMemberUID ?? 0,
                           Year = comm.Year
                       };
            if (ent.AppContactPersonUID > 0)
                list = list.Where(a=>a.AppCPUID==ent.AppContactPersonUID);
            if (ent.ContactPersonUID > 0)
                list = list.Where(a=>a.TblCPUID==ent.ContactPersonUID);
            if (AddDeletedForSelect)
                list = list.Where(a=>a.IsDeleted==ent.Deleted);
            return list.ToList();
        }

        public int DeleteByKeyList(List<int> list)
        {
            foreach (var temp in list)
            {
                var result = model.app_tblCPerson_CommMember.Where(a => a.AppCperson_CommMemberUID == temp).FirstOrDefault();
                if (result != null)
                    model.app_tblCPerson_CommMember.DeleteObject(result);
            }
            return model.SaveChanges();
        }
    }
}
